package org.hzz.application.command;

import io.gitee.pkmer.convention.page.PageResponse;
import io.gitee.pkmer.convention.page.query.FilterCondition;
import io.gitee.pkmer.ddd.shared.command.CommandWithMark;
import io.gitee.pkmer.ddd.shared.service.CmdService;
import lombok.Data;
import lombok.Value;
import lombok.experimental.Accessors;
import org.hzz.application.handler.CodePageQueryCmdHandler;
import org.hzz.promotions.types.constants.ExchangeCodeFields;
import org.hzz.promotions.types.enums.code.CodeStatus;
import org.hzz.promotions.types.req.ExchangeCodePageQuery;
import org.hzz.promotions.types.vo.ExchangeCodeVo;

import java.util.List;

import static io.gitee.pkmer.convention.page.query.FilterCondition.Operation.EQ;

/**
 * @author <a href="mailto:1193094618@qq.com">pkmer</a>
 * <br>
 * <a href = "https://gitee.com/developeros/videos-online">Code Repository</a>
 * At 2024/9/1
 * 兑换码查询命令
 * {@link CodePageQueryCmdHandler}
 */
@Data
@Accessors(chain = true)
@Value(staticConstructor = "commandOf")
public class CodePageQueryCmd implements CommandWithMark {

    public static final String MARK = CodePageQueryCmd.class.getName();
    ExchangeCodePageQuery pageQuery;

    @Override
    public String mark() {
        return MARK;
    }

    public PageResponse<ExchangeCodeVo> executeWithResult(CmdService cmdService){
        handleCondition();
        return cmdService.<PageResponse<ExchangeCodeVo>>handleCommandWithResult(this);
    }

    private void handleCondition(){
        CodeStatus codeStatus = pageQuery.getCodeStatus();
        Long couponId = pageQuery.getCouponId();

        List<FilterCondition> conditions = FilterCondition.createCriteria()
                .and(ExchangeCodeFields.EXCHANGE_TARGET_ID, EQ, couponId)
                .and(ExchangeCodeFields.STATUS, EQ, codeStatus)
                .build();

        pageQuery.setFilters(conditions);
    }
}
